﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SacNetUG.Models;

namespace SacNetUG.Filters
{
    public class RsvpFilterAttribute : ActionFilterAttribute
    {
        public string QueryDate { get; set; }

        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            var qDate = DateTime.Today;
            if(!string.IsNullOrEmpty(QueryDate))
                qDate = DateTime.Parse(QueryDate);

            //must be with 7 days before the event
            var repo = new SacNetUGRepository();
            var events = repo.GetFutureEvents().Take((1));

            if(events.Count() == 0)
            {
                filterContext.Controller.ViewData["FutureEvents"] = new SelectList(new List<string>());
                return;
            }

            //TODO lookup the next event
            var eventDts = events.First().StartDate;

            var diff = eventDts.Subtract(qDate);

            SelectList items;

            if (Enumerable.Range(0, 8).Contains(diff.Days))
                items = new SelectList(events, "EventId", "Name");
            else
                items = new SelectList(new List<string> { "No Event" });

            filterContext.Controller.ViewData["FutureEvents"] = items;
        }
    }
}
